/* @(#)GSBalancedDetailAction.java
 * 
 *  Project:GS
 *  
 *  Modify  Information:
 *  ===============================================
 *   Author          Date            Description
 *  -------------- ---------------  ---------------
 *  Jenny  Li      2007-5-12          新增
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 *
 */
package com.hitrust.gs.action;

import java.math.BigDecimal;
import java.util.ArrayList;

import com.hitrust.gs.bean.GSBalanceMsgBean;
import com.hitrust.gs.bean.GSStocksOtherBean;
import com.hitrust.gs.business.GSOrderProcessor;
import com.hitrust.gs.business.GSStocksProcesser;
import com.hitrust.gs.dao.GSBalanceMsgDAO;
import com.hitrust.gs.dao.GSOrderDetailDAO;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.service.GSBaseManager;
import com.hitrust.gs.util.date.DateUtil;
import com.hitrust.gs.util.page.ResultProcess;
import com.hitrust.gs.vo.GSUnBalanceDetailQryVO;

/**
 * @author Administrator
 *
 */
public class GSBalancedDetailAction extends GSBaseAction {
    private GSStocksProcesser stpro=new GSStocksProcesser();
    /* (non-Javadoc)
     * @see com.hitrust.gs.service.GSBaseAction#execute(com.hitrust.gs.service.GSBaseForm)
     */
    public String execute(GSBaseForm aForm) throws Exception {
        String tDeptId  = aForm.getRequest().getParameter("deptId");
        String tBalanceYM = aForm.getRequest().getParameter("balanceYM");
        String tDeptCategory = aForm.getRequest().getParameter("deptCategory");
        
        GSUnBalanceDetailQryVO tVO = new GSUnBalanceDetailQryVO();
        tVO.deptId = tDeptId;
        tVO.deptCategory = tDeptCategory;
        tVO.balanceYM = tBalanceYM;
        GSBalanceMsgDAO tBalMsgDAO = new GSBalanceMsgDAO();
        ArrayList tBalList = tBalMsgDAO.queryBalanceInfo(tVO.deptId,tVO.balanceYM);
        if(tBalList.size() == 1){
            GSBalanceMsgBean tBean = (GSBalanceMsgBean)tBalList.get(0);
            tVO.endBalDate = DateUtil.splitSpecChar(tBean.getGs_end_date());
        }
        //取得分页参数
        String[] tStr = this.getPageParams(aForm,tVO);
        
        GSOrderProcessor tOrderProc = new GSOrderProcessor();
        ArrayList tList = tOrderProc.getBalancedAssociatedOrderDetail(tDeptId,tDeptCategory,tBalanceYM,Integer.parseInt(tStr[0]),Integer.parseInt(tStr[1]));
        String dou1="0";
        BigDecimal d1=new BigDecimal(0);
        BigDecimal d2=new BigDecimal(0);
        if (tList.size()>0) {
            for (int u = 0; u < tList.size(); u++) {
                String[] temp=(String[]) tList.get(u);
                d2=new BigDecimal(Double.parseDouble(temp[17])*Double.parseDouble(temp[18]));
                d1=d1.add(d2);
            }
            dou1=String.valueOf(d1.doubleValue());
        }
        aForm.getRequest().setAttribute("ordersum",dou1);
        ResultProcess tRsProc = new ResultProcess();
        tRsProc.setResult(tList);
        aForm.getRequest().setAttribute("orderdetail",tRsProc);
        //保存查询参数
        aForm.getRequest().setAttribute("deptId",tDeptId);
        aForm.getRequest().setAttribute("balanceYM",tBalanceYM);
        aForm.getRequest().setAttribute("deptCategory",tDeptCategory);
        
        aForm.getRequest().setAttribute("pageSize",tStr[1]);
        aForm.getRequest().setAttribute("totalRecords",tStr[2]);
      //显示是否包含压库应付
        String[] str={tDeptId,tBalanceYM,GSBaseConstants.BALANCE_MODE2};
        ArrayList arstocks=stpro.getStocksDetailByMonthProvid(str);
        String dou2="0";
        BigDecimal dl1=new BigDecimal(0);
        BigDecimal dl2=new BigDecimal(0);
        if (arstocks.size()>0) {
            for (int i = 0; i < arstocks.size(); i++) {
                GSStocksOtherBean obean=(GSStocksOtherBean) arstocks.get(i);
                dl2=new BigDecimal(obean.getGs_detail_count()*obean.getGs_detail_price());
                dl1=dl1.add(dl2);
            }
            dou2=String.valueOf(dl1.doubleValue());
        }
        aForm.getRequest().setAttribute("stockssum",dou2);
        return GSBaseConstants.FORWARD_SUCCESS;
    }
       private String[] getPageParams(GSBaseForm aForm,GSUnBalanceDetailQryVO aVO) throws Exception{
            String tStr[] = new String[3];
            int totalRecords;
            int pageNo;
            int pageSize;
            String tPage = GSBaseConstants.tPage;
            //根据页面传入的查询参数查询订单信息
            if(aForm.getRequest().getParameter("choice") == null || "".equals(aForm.getRequest().getParameter("choice"))){
                GSOrderDetailDAO tempOrderDAO = new GSOrderDetailDAO();
                totalRecords = tempOrderDAO.queryDetailAssociatedBalancedCount(aVO.deptId,aVO.deptCategory,aVO.endBalDate,aVO.balanceYM);
                // 取得系统默认的页大小
                pageSize = Integer.parseInt(tPage);
                // PageNo=1
                pageNo = 1;
                int n = (totalRecords % pageSize == 0 )? totalRecords / pageSize:totalRecords / pageSize + 1;
                if (pageNo>n) {
                    pageNo=n;
                }
                tStr[0] = "" + pageNo;
                tStr[1] = "" + pageSize;
                tStr[2] = "" + totalRecords;
            }else{
                tStr = GSBaseManager.getPageNoAndPageSize(aForm);
            }
            return tStr;
        }

}
